//  Copyright (c) 2013年 mafengwo All rights reserved.
//  Created by hanfeng on 12-12-13.

#import "NSDate+Addition.h"

static NSDateFormatter *s_relativeDateFormatter = nil;

@implementation NSDate (Addition)

- (NSString *)relativeFormatStringWithSecond:(BOOL)sec
{
    NSTimeInterval tI = [[NSDate date] timeIntervalSinceDate:self];
    if (tI < 60) {
        if(sec ==NO) {
            return NSLocalizedString(@"1分钟内", @"");
        }
        return[NSString stringWithFormat:NSLocalizedString(@"%d秒钟前", @""), (int)tI];
    }
    if (tI < 3600) {
        return[NSString stringWithFormat:NSLocalizedString(@"%d分钟前", @""), (int)(tI/60)];
    }
    if (tI < 10800) { //just tip 2 hours ago
        return[NSString stringWithFormat:NSLocalizedString(@"%d小时前", @""), (int)tI/3600];
    }

    if (s_relativeDateFormatter == nil) {
        s_relativeDateFormatter =[[NSDateFormatter alloc] init];
        [s_relativeDateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [s_relativeDateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [s_relativeDateFormatter setDoesRelativeDateFormatting:YES];
//        s_relativeDateFormatter.dateFormat = @"yyyy.MM.dd HH:mm:ss";
        [s_relativeDateFormatter setTimeZone:[NSTimeZone localTimeZone]];

        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];
        [s_relativeDateFormatter setLocale:locale];
    }
    NSString* relativeFormattedString = [s_relativeDateFormatter stringForObjectValue:self];
    return relativeFormattedString;
}

- (BOOL)isToday
{
    static NSTimeInterval dayDuration = (24*60*60);
    
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval time = [self timeIntervalSince1970];
    
    return (unsigned long long)(now/dayDuration) == (unsigned long long)(time/dayDuration);
}

- (BOOL)isEarlierThan:(NSDate *)aDate
{
    return [aDate timeIntervalSinceDate:self] > 0;
}

+(NSString *)currentTimeStamp{
    NSDate *date=[NSDate date];
    NSTimeInterval timeInterl=[date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%f",timeInterl];
}

@end
